/**
 * 
 */
package cn.gov.msa.ws;

import java.util.List;

import org.springframework.context.ApplicationContext;

import com.github.pagehelper.PageInfo;

import cn.gov.msa.collection.SharedLibraryService;
import cn.gov.msa.model.BaseShip;
import cn.gov.msa.model.Port;
import cn.gov.msa.model.VSelectBusinessQuery;
import cn.gov.msa.model.Verify;
import cn.gov.msa.service.BaseShipService;
import cn.gov.msa.service.PortService;
import cn.gov.msa.service.QueryService;
import cn.gov.msa.service.VerifyService;
import cn.gov.msa.service.impl.VerifyServiceImpl;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author Administrator
 *
 */
public class SystemService {

	/*
	 * 船舶查询
	 * */
	public static String queryShip(String params) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
	    JSONObject jsonResult = new JSONObject();
		
		try{
			JSONObject jaShip = JSONObject.fromObject(SharedLibraryService.queryShip(JSONObject.fromObject(params)));
			if(jaShip.getInt("total") > 0){
				result = 1;
				info = InfoObjUtil.Success_save_OK;
			}
			jsonResult.put("datas", jaShip);
		}
		catch(Exception e){
			e.printStackTrace();
        	result = 2;
        	info = InfoObjUtil.Exption_msg;
		}
		

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);

		
		return jsonResult.toString();
	}
	
	/**
	 * 船舶校验
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String check(String params) {
		return SharedLibraryService.check(JSONObject.fromObject(params));
	}

	public static String queryBerth(String params) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
	    JSONObject jsonResult = new JSONObject();
		
		try{
			JSONObject jaShip = JSONObject.fromObject(SharedLibraryService.queryBerth(JSONObject.fromObject(params)));
			if(jaShip.getInt("total") > 0){
				result = 1;
				info = InfoObjUtil.Success_save_OK;
			}
			jsonResult.put("datas", jaShip);
		}
		catch(Exception e){
			e.printStackTrace();
        	result = 2;
        	info = InfoObjUtil.Exption_msg;
		}
		
    	jsonResult.put("result", result);
    	jsonResult.put("info", info);

		
		return jsonResult.toString();
	}

	public static String queryPort(ApplicationContext ctx,String params) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
	    JSONObject jsonResult = new JSONObject();
		
		try{
			JSONObject param = JSONObject.fromObject(params);
			PortService portService = (PortService) ctx.getBean("PortService");  
	         
			String orgcode = "";
			if(param.has("orgcode"))
				orgcode = param.getString("orgcode");
			
			List<Port> portList = portService.selectByOrgcode(orgcode);
	        
			if(portList.size() > 0){
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				
				jsonResult.put("datas", portList);
			}
		}
		catch(Exception e){
			e.printStackTrace();
        	result = 2;
        	info = InfoObjUtil.Exption_msg;
		}
		

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);

		
		return jsonResult.toString();
	}
	
	public static String queryVerify(ApplicationContext ctx,String params) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {
			JSONObject param = JSONObject.fromObject(params);
			VerifyService verifyService = (VerifyService) ctx.getBean("VerifyService");  
			String verufyList = verifyService.getVerifyList(params);
			result = 1;
			info = InfoObjUtil.Success_select_OK;
			jsonResult.put("datas", verufyList);
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	/**
	 * (本地)新增船舶
	 * @param ctx
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String addLocalShip(ApplicationContext ctx, String params) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_save_no;
		JSONObject jsonResult = new JSONObject();

		try {
			BaseShipService baseShipService = (BaseShipService) ctx.getBean("BaseShipService");  
			JSONObject param = JSONObject.fromObject(params);
			BaseShip ship = (BaseShip) JSONObject.toBean(param, BaseShip.class);
			result = baseShipService.insertSelective(ship);
			if(result > 0){
				info = InfoObjUtil.Success_save_OK;
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	/**
	 * (本地)查询船舶
	 * @param ctx
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String getLocalShip(ApplicationContext ctx, String params) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();
		
		try {
			BaseShipService baseShipService = (BaseShipService) ctx.getBean("BaseShipService");  
			
			BaseShip ship = (BaseShip) JSONObject.toBean(JSONObject.fromObject(params), BaseShip.class);
			
			List<BaseShip> list = baseShipService.selectData(ship);
			if(list != null && list.size() > 0){
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", JSONObject.fromObject(list.get(0)));
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}
		
		jsonResult.put("result", result);
		jsonResult.put("info", info);
		
		return jsonResult.toString();
	}
}
